package com.alan.camunda.boot.delegate;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.camunda.bpm.engine.variable.Variables;
import org.camunda.bpm.engine.variable.value.BooleanValue;
import org.springframework.stereotype.Component;

/**
 * 测试保存临时变量，临时变量不会被保存到数据库
 * Camunda 提供了 Variables 工具类来创建临时变量
 */
@Slf4j
@Component("transientVariablesDelegate")
public class TransientVariablesDelegate implements JavaDelegate {

    @Override
    public void execute(DelegateExecution delegateExecution) throws Exception {
        // 创建一个临时的变量，不被持久化
        BooleanValue bv = Variables.booleanValue(true, true);
        delegateExecution.setVariableLocal("_TT", bv);

        // 创建一个 JSONObject 的临时变量
        JSONObject result = new JSONObject();
        result.put("code", 0);
        result.put("data", "123456");

        delegateExecution.setVariableLocal("_R", Variables.objectValue(result, true));

    }


}
